{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tips & Tricks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"toc\" style=\"margin-top: 1em;\"><ul class=\"toc-item\"><li><span><a href=\"#01.查看帮助\" data-toc-modified-id=\"01.查看帮助-1.1\">01.查看帮助</a></span></li><li><span><a href=\"#02.快捷键P打开命令面板\" data-toc-modified-id=\"02.快捷键P打开命令面板-1.2\">02.快捷键P打开命令面板</a></span></li><li><span><a href=\"#03.末句函数不输出\" data-toc-modified-id=\"03.末句函数不输出-1.3\">03.末句函数不输出</a></span></li><li><span><a href=\"#04.使用！运行Shell命令\" data-toc-modified-id=\"04.使用！运行Shell命令-1.4\">04.使用！运行Shell命令</a></span></li><li><span><a href=\"#05.在同一个notebook里运行R和Python\" data-toc-modified-id=\"05.在同一个notebook里运行R和Python-1.5\">05.在同一个notebook里运行R和Python</a></span></li><li><span><a href=\"#06.使用Cpython加速\" data-toc-modified-id=\"06.使用Cpython加速-1.6\">06.使用Cpython加速</a></span></li><li><span><a href=\"#07.使用Fortran\" data-toc-modified-id=\"07.使用Fortran-1.7\">07.使用Fortran</a></span></li><li><span><a href=\"#08.多指针编辑\" data-toc-modified-id=\"08.多指针编辑-1.8\">08.多指针编辑</a></span></li><li><span><a href=\"#09.使用RISE制作PPT\" data-toc-modified-id=\"09.使用RISE制作PPT-1.9\">09.使用RISE制作PPT</a></span></li><li><span><a href=\"#10.Cach-&amp;-History\" data-toc-modified-id=\"10.Cach-&amp;-History-1.10\">10.Cach &amp; History</a></span></li><li><span><a href=\"#11.Autocall\" data-toc-modified-id=\"11.Autocall-1.11\">11.Autocall</a></span></li><li><span><a href=\"#12.使用reconnect\" data-toc-modified-id=\"12.使用reconnect-1.12\">12.使用reconnect</a></span></li><li><span><a href=\"#13.使用Jupyter-Dashboards\" data-toc-modified-id=\"13.使用Jupyter-Dashboards-1.13\">13.使用Jupyter Dashboards</a></span></li><li><span><a href=\"#14.jupyter-换装\" data-toc-modified-id=\"14.jupyter-换装-1.14\">14.jupyter 换装</a></span></li><li><span><a href=\"#15.自定义样式\" data-toc-modified-id=\"15.自定义样式-1.15\">15.自定义样式</a></span></li><li><span><a href=\"#16.如何避免notebook卡顿？\" data-toc-modified-id=\"16.如何避免notebook卡顿？-1.16\">16.如何避免notebook卡顿？</a></span></li><li><span><a href=\"#17.Kernels-for-Python-2-and-3\" data-toc-modified-id=\"17.Kernels-for-Python-2-and-3-1.16\">17.Kernels for Python 2 and 3</a></span></li></ul></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 01.查看帮助"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "?list  or  list?\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "详细帮助"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "??str  or  str??\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "? 的格式可以查询动态对象信息"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "建议使用？在前的格式，？在后的格式有时无效"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看完整文档可使用help函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "help(str)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 02.快捷键P打开命令面板"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 03.末句函数不输出"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在cell结尾使用“；”号可以在绘图时不输出末句函数<br>\n",
    "请动手运行这两种写法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "%matplotlib inline\n",
    "from matplotlib import pyplot as plt\n",
    "import numpy\n",
    "x = numpy.linspace(0, 1, 1000)**1.5\n",
    "plt.hist(x)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "%matplotlib inline\n",
    "from matplotlib import pyplot as plt\n",
    "import numpy\n",
    "x = numpy.linspace(0, 1, 1000)**1.5\n",
    "plt.hist(x);\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 04.使用！运行Shell命令"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "!ls\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 05.在同一个notebook里运行R和Python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`pip install rpy2 `"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 06.使用Cpython加速"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`!pip install cython fortran-magic `\n",
    "\n",
    "```python\n",
    "In [ ]: %load_ext Cython\n",
    "In [ ]: %%cython\n",
    "        def myltiply_by_2(float x):\n",
    "            return 2.0 * x\n",
    "In [ ]: myltiply_by_2(23.)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 07.使用Fortran"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "详见 http://arogozhnikov.github.io/2015/11/29/using-fortran-from-python.html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "In [ ]: %load_ext fortranmagic\n",
    "In [ ]: %%fortran\n",
    "        subroutine compute_fortran(x, y, z)\n",
    "            real, intent(in) :: x(:), y(:)\n",
    "            real, intent(out) :: z(size(x, 1))\n",
    "\n",
    "            z = sin(x + y)\n",
    "\n",
    "        end subroutine compute_fortran\n",
    "In [ ]: compute_fortran([1, 2, 3], [4, 5, 6])\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 08.多指针编辑"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "按住Ctrl点击要编辑部位即可获得多指针 <br>\n",
    "按住Alt和鼠标左键，滑动鼠标，即可获得连续多行的多指针"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 09.使用RISE制作PPT"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "详见 https://github.com/damianavila/RISE<br>\n",
    "<img src='images/29.gif'>\n",
    "安装并激活"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`$ pip install RISE`\n",
    "\n",
    "`$ jupyter-nbextension install rise --py --sys-prefix`\n",
    "\n",
    "`$ jupyter-nbextension enable rise --py --sys-prefix`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10.Cach & History"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "_i, _ii, _iii    : Previous, next previous, next next previous input\n",
    "_i4, _ih[2:5]    : Input history line 4, lines 2-4\n",
    "exec _i81        : Execute input history line #81 again\n",
    "%rep 81          : Edit input history line #81\n",
    "_, __, ___       : previous, next previous, next next previous output\n",
    "_dh              : Directory history\n",
    "_oh              : Output history\n",
    "%hist            : Command history of current session.\n",
    "%hist -g foo     : Search command history of (almost) all sessions for 'foo'.\n",
    "%hist -g         : Command history of (almost) all sessions.\n",
    "%hist 1/2-8      : Command history containing lines 2-8 of session 1.\n",
    "%hist 1/ ~2/     : Command history of session 1 and 2 sessions before current.\n",
    "%hist ~8/1-~6/5  : Command history from line 1 of 8 sessions ago to\n",
    "                   line 5 of 6 sessions ago.\n",
    "%edit 0/         : Open editor to execute code with history of current session.\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 11.Autocall"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "f 1,2            : f(1,2)  # Off by default, enable with %autocall magic.\n",
    "/f 1,2           : f(1,2) (forced autoparen)\n",
    ",f 1 2           : f(\"1\",\"2\")\n",
    ";f 1 2           : f(\"1 2\")\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 12.使用reconnect"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当你发现一个正在执行命令的页面很久没有发生变化时，请试一试kernel菜单中reconnect按钮"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 13.使用Jupyter Dashboards"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='images/31.png'>\n",
    "下载"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "install using pip from pypi and then activate the extension <br>\n",
    "`pip install jupyter_dashboards` <br>\n",
    "`jupyter dashboards quick-setup --sys-prefix` <br>\n",
    "\n",
    "install using conda from conda-forge, no activation required <br>\n",
    "`conda install jupyter_dashboards -c conda-forge` <br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "详见链接 <br>\n",
    "https://github.com/jupyter/dashboards <br>\n",
    "http://jupyter-dashboards-layout.readthedocs.io/en/latest/getting-started.html <br>    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 14.jupyter 换装"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "原装 jupyter 太丑？<br>\n",
    "使用jupyter themes吧 <br>\n",
    "https://github.com/dunovank/jupyter-themes <br>\n",
    "<img src='images/30.png'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 15.自定义样式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你可以选择直接更改jupyter的css和js配置<br>\n",
    "如果这样比较有难度的话，推荐使用Chrome的修改网页样式的插件。这种插件很多，这里就不推荐了。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 16.如何避免notebook卡顿？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 避免书写太大的.ipynb文件。切记，notebook是演草纸，对于大型工程，及时将演草纸中的内容誊写到卷子上(.py文件)才是正确的方式\n",
    "* 保持良好的文件夹结构\n",
    "* 不要装过多插件\n",
    "* 网络稳定(jupyter 服务器)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 17.Kernels for Python 2 and 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "已经有了Python3的jupyter，想再下个Python2的核心？\n",
    "\n",
    "很简单"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "python2 -m pip install ipykernel\n",
    "python2 -m ipykernel install --user\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "想下Python3的话同理，将python2换成python3即可"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  },
  "nbTranslate": {
   "displayLangs": [
    "*"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "en",
   "targetLang": "fr",
   "useGoogleTranslate": true
  },
  "toc": {
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": true,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
